package com.beiing.tianshuai.tianshuai.widget;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.beiing.tianshuai.tianshuai.R;
import com.beiing.tianshuai.tianshuai.util.DensityUtils;

/**
 * Created by Administrator on 2018/1/12 0012.
 */

public class CustomSingleCheckDialog extends Dialog {

    private String[] items;
    private Context context;
    private String title;

    private LinearLayout itemsContainer;
    private TextView tvTitle;

    public CustomSingleCheckDialog(@NonNull Context context, String[] items, String title) {
        super(context);
        this.context = context;
        this.items = items;
        this.title = title;
    }

    public CustomSingleCheckDialog(@NonNull Context context, int themeResId, String[] items, String title) {
        super(context, themeResId);
        this.context = context;
        this.items = items;
        this.title = title;
    }

    protected CustomSingleCheckDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_custom_single_check);
        //设置标题
        tvTitle = findViewById(R.id.tv_dialog_title);
        tvTitle.setText(title);
        //设置items
        itemsContainer = findViewById(R.id.ll_items_container);
        for (int i = 0; i < items.length; i++) {
            // 分割线
            View v = new View(context);
            v.setBackgroundColor(0xFFEFEFEF);
            LinearLayout.LayoutParams layoutParams1 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, DensityUtils.dp2px(context, 0.5f));
            v.setLayoutParams(layoutParams1);
            itemsContainer.addView(v);
            // 选项
            TextView textView = new TextView(context);
            textView.setText(items[i]);
            final int finalI = i;
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, DensityUtils.dp2px(context, 50f));
            textView.setLayoutParams(layoutParams);
            textView.setTextSize(14);
            textView.setTextColor(0xFF000000);
            textView.setGravity(Gravity.CENTER);
            textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnItemClickListener != null) {
                        mOnItemClickListener.onItemClick(finalI);
                    }
                }
            });
            itemsContainer.addView(textView);
        }
    }

    public interface OnItemClickListener {
        void onItemClick(int position);
    }

    private OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }
}
